#{extends 'organization.html' /}
<h1 class="viewTitle">Add a user as an organizer to an entity</h1>
<body onload="entityAndUserChecked()"></body>
<div class="viewContent">
	#{if entities.size()==0}
		<div>You are not the organization lead/ not an organizer in the given organization.</div>
	#{/if}
	#{else}
		#{if message != null && !message.equals("")}
			<div><p>${message}</p></div><br>
		#{/if}
		#{if error != null && !error.equals("")}
			<div><p class="error">${error}</p></div><br>
		#{/if}
		
		<div class="organizers">
			<div>Choose the entity:</div>
			#{form @Application.submitOrganizerToEntity()}   
				#{list items:entities, as:'entity'}
				<div>
					<input type="radio" name="entityID" value=${entity.id} onclick="entityAndUserChecked()" />
						${entity.organization.name} entity ${entity.name}
					<br/>
				</div>
				#{/list}
				<br>
				<div>Choose the user:</div>
				#{list items:users, as:'user'}
				<div class="organizer">
					<input type="radio" name="username" value=${user.userName} onclick="entityAndUserChecked()" />
					${user.userName}
					<br/>
				</div>
				#{/list}
				<br/>
				<script type="text/javascript">
				<!--
				
				function entityAndUserChecked() {
   					var entityElements = document.getElementsByName("entityID");
					var userElements = document.getElementsByName("username");
					var submitElement = document.getElementById("submit");
					
					var entityChecked = false;
					var userChecked = false;
					
					for (var i = 0; i < entityElements.length; i++) {
					    if (entityElements[i].checked) {
        					entityChecked = true;
							break;
						}
					}
					
					for (var i = 0; i < userElements.length; i++) {
					    if (userElements[i].checked) {
        					userChecked = true;
							break;
						}
					}

					if (entityChecked && userChecked)
						submitElement.disabled = false;
					else submitElement.disabled = true;
				}
				//-->
				</script>
				<input style="float:left;" type="submit" value="Add Organizer To Entity" id="submit"/>
			#{/form}
			#{form @Users.viewOrganizationsOfUser()}
			<input style="float:left;" type="submit" value=" Cancel "></input>
			<div class="clear"></div>
			#{/form}
		</div>
	#{/else}
</div>